/***
 * SSO sever 配置类。
 * */

package org.web.common.sso.server.config;

import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;


@Configuration
@ConfigurationProperties(
        value = "org.web.sso.server")
@Getter
@Setter
@Scope("singleton")
public class SSOServerConfig implements InitializingBean {

    private static final Logger logger = LoggerFactory.getLogger(SSOServerConfig.class);

    // 是否基于缓存进行处理。
    private Boolean hasRedis = false;

    // 刷新缓存的时间，其目的是减少对缓存的写操作，单位分钟,小于该时间，不做刷新操作。非必填项，默认1分钟，不建议修改。不能大于inCashTime，如果小于inCashTime，调整成默认值。
    private Integer refreshInCashTime = 1;

    // 缓存时间，单位分钟，不设置，默认为5分钟。程序控制最大60分钟,最小1分钟。
    private Integer inCashTime = 5;

    private static final int MAX_IN_CASH_TIME = 60;

    // 主域名,格式是 .web.com
    private String domain = null;

    /***
     * 参数合肥性校验。
     * */
    @Override
    public void afterPropertiesSet() throws Exception {
        if (inCashTime > MAX_IN_CASH_TIME) {
            throw new Exception("初始化异常，inCashTime 不要超过 " + MAX_IN_CASH_TIME + ". 当前设置为 : " + inCashTime);
        } else if (inCashTime < 0) {
            throw new Exception("初始化异常，inCashTime 不要超小于零. 当前设置, inCashTime : " + inCashTime);
        } else if (refreshInCashTime < 0) {
            throw new Exception("初始化异常，refreshInCashTime 不要超小于零. 当前设置, refreshInCashTime : " + refreshInCashTime);
        }
        if (refreshInCashTime > inCashTime) {
            throw new Exception("初始化异常，refreshInCashTime 不能大于inCashTime. 当前设置为, inCashTime : " + inCashTime + " ,refreshInCashTime :" + refreshInCashTime);
        }
        if (StringUtils.isEmpty(domain)) {
            throw new Exception("初始化异常，domain 参数不能为空 ");
        }

        logger.info("refreshInCashTime " + refreshInCashTime);
        logger.info("inCashTime " + inCashTime);
    }


}
